﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using Maticsoft.Common;
using Microsoft.Win32;
using CustomsSystem.Model;
namespace WEB.T_all
{
    public partial class FeeWF : System.Web.UI.Page
    {
        T_AllManage tALL = new T_AllManage();
        SheetInfoManage SM = new SheetInfoManage();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        FeeWF_Excel dataToExcel = new FeeWF_Excel();
        CustomerManage CM = new CustomerManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime A = new DateTime(DateTime.Today.Year, DateTime.Today.AddMonths(1).Month, 1);
            int month = Convert.ToInt32(A.Date.Month.ToString());
            if (!IsPostBack)
            {
                tbGency1.Text = A.ToShortDateString();
                tbGency.Text = A.AddMonths(-3).Date.ToShortDateString();
                rbt2.Checked = true;

            }
            bind();
            if (gvTALL.Rows.Count == 0)
            {
                Label7.Text = "没有数据";
            }
            else
            {
                Label7.Text = "";
            }
            this.tbGency.Attributes.Add("onClick", "new WdatePicker()");
            this.tbGency1.Attributes.Add("onClick", "new WdatePicker()");
        }

        protected void gvTALL_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            LableVoid();
        }

        protected void bind()
        {
            DataSet ds = new DataSet();
            T_AllManage t_all = new T_AllManage();
            string payedID = "";
            if (rbt1.Checked == true)
            {
                payedID = "='1'";
                gvTALL.DataSource = t_all.GetFeeWF(Convert.ToDateTime(tbGency.Text), Convert.ToDateTime(tbGency1.Text), tbCus.Text.Trim(), payedID);
                gvTALL.DataBind();
                LableVoid();
            }
            if (rbt2.Checked == true)
            {
                payedID = " ='0' ";
                gvTALL.DataSource = t_all.GetFeeWF(Convert.ToDateTime(tbGency.Text), Convert.ToDateTime(tbGency1.Text), tbCus.Text.Trim(), payedID);
                gvTALL.DataBind();
                for (int i = 0; i < gvTALL.Rows.Count; i++)
                {
                    gvTALL.Rows[i].Cells[7].Text = "0.00";
                }
                LableVoid();
            }
            if (rbt3.Checked == true)
            {
                payedID = " ='2' ";
                gvTALL.DataSource = t_all.GetFeeWF(Convert.ToDateTime(tbGency.Text), Convert.ToDateTime(tbGency1.Text), tbCus.Text.Trim(), payedID);
                gvTALL.DataBind();
                LableVoid();
            }
        }

        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetCustomersName(string prefixText, int count)
        {
            CustomerManage cusManage = new CustomerManage();
            List<string> strArray = new List<string>();
            //string[] strArray = { "中国石油公司","中顺洁柔","中山技术",
            //"捷达捷达捷达达","时代复分","幅度为爱"};
            List<Customer> cusList = cusManage.GetModelList("");
            foreach (Customer aCus in cusList)
            {
                strArray.Add(aCus.CustomerName);
            }
            return (from m in strArray
                    where m.Contains(prefixText)
                    select m).Take(count).ToArray();
        }


        protected void LableVoid()
        {
            decimal sum = 0, sum1 = 0;
            if (rbt2.Checked == true)
            {
                //当按钮是未销帐--计算统计
                for (int i = 0; i < gvTALL.Rows.Count; i++)
                {
                    if (gvTALL.Rows[i].Cells[6].Text == "&nbsp;")
                    {
                        gvTALL.Rows[i].Cells[6].Text = "0.00";
                    }
                    if (gvTALL.Rows[i].Cells[7].Text == "&nbsp;")
                    {
                        gvTALL.Rows[i].Cells[7].Text = "0.00";
                    }
                    gvTALL.Rows[i].Cells[8].Text = Convert.ToString(Convert.ToDecimal(gvTALL.Rows[i].Cells[7].Text) - Convert.ToDecimal(gvTALL.Rows[i].Cells[6].Text));
                    sum = sum + Convert.ToDecimal(gvTALL.Rows[i].Cells[6].Text);
                    sum1 = sum1 + Convert.ToDecimal(gvTALL.Rows[i].Cells[7].Text);
                }
                Label1.Text = sum.ToString();
                Label2.Text = "0";
                Label3.Text = "-" + Convert.ToString(sum - sum1);
            }
            else
            {
                //计算统计
                for (int i = 0; i < gvTALL.Rows.Count; i++)
                {
                    if (gvTALL.Rows[i].Cells[6].Text == "&nbsp;")
                    {
                        gvTALL.Rows[i].Cells[6].Text = "0.00";
                    }
                    if (gvTALL.Rows[i].Cells[7].Text == "&nbsp;")
                    {
                        gvTALL.Rows[i].Cells[7].Text = "0.00";
                    }
                    gvTALL.Rows[i].Cells[8].Text = Convert.ToString(Convert.ToDecimal(gvTALL.Rows[i].Cells[7].Text) - Convert.ToDecimal(gvTALL.Rows[i].Cells[6].Text));
                    sum = sum + Convert.ToDecimal(gvTALL.Rows[i].Cells[6].Text);
                    sum1 = sum1 + Convert.ToDecimal(gvTALL.Rows[i].Cells[7].Text);
                }
                Label1.Text = sum.ToString();
                Label2.Text = sum1.ToString();
                Label3.Text = "-" + Convert.ToString(sum - sum1);
            }
        }

        protected void Check_Click(object sender, EventArgs e)
        {
            bind();
        }

        protected void Excel_Click(object sender, EventArgs e)
        {
            string payedID = "";
            if (rbt1.Checked == true)
            {
                payedID = "='1'";
                DataSet ds = new DataSet();
                if (gvTALL.Rows.Count > 0)
                {
                    DateTime KAI, MO;
                    if (tbGency.Text == "")
                    {
                        KAI = Convert.ToDateTime("1900-1-1").Date;
                    }
                    else
                    {
                        KAI = Convert.ToDateTime(tbGency.Text).Date;
                    }

                    if (tbGency1.Text == "")
                    {
                        MO = Convert.ToDateTime("2099-1-1").Date;
                    }
                    else
                        MO = Convert.ToDateTime(tbGency1.Text).Date;

                    string Cname = tbCus.Text;
                    //取得列名
                    ds = tALL.GetFeeWF(KAI, MO, Cname, payedID);
                    foreach (DataColumn dc in ds.Tables[0].Columns)
                    {
                        dt.Columns.Add(dc.ColumnName, dc.DataType);
                    }
                    //读取数据
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        object[] arrdr = new object[ds.Tables[0].Columns.Count];
                        ds.Tables[0].Rows[i].ItemArray.CopyTo(arrdr, 0);
                        dt.Rows.Add(arrdr);
                    }

                    //生成excel
                    string FilePath = Server.MapPath("~/document/Excel/");
                    string file = dataToExcel.OutputExcel(dt, Cname, KAI, MO, payedID, "明细帐表", FilePath);
                    DownloadFile(file);
                    return;
                }
            }

            if (rbt2.Checked == true)
            {
                payedID = "='0' ";
                DataSet ds = new DataSet();
                if (gvTALL.Rows.Count > 0)
                {
                    DateTime KAI, MO;
                    if (tbGency.Text == "")
                    {
                        KAI = Convert.ToDateTime("1900-1-1").Date;
                    }
                    else
                    {
                        KAI = Convert.ToDateTime(tbGency.Text).Date;
                    }

                    if (tbGency1.Text == "")
                    {
                        MO = Convert.ToDateTime("2099-1-1").Date;
                    }
                    else
                        MO = Convert.ToDateTime(tbGency1.Text).Date;

                    string Cname = tbCus.Text;
                    //取得列名
                    ds = tALL.GetFeeWF(KAI, MO, Cname, payedID);
                    foreach (DataColumn dc in ds.Tables[0].Columns)
                    {
                        dt.Columns.Add(dc.ColumnName, dc.DataType);
                    }
                    //读取数据
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        object[] arrdr = new object[ds.Tables[0].Columns.Count];
                        ds.Tables[0].Rows[i].ItemArray.CopyTo(arrdr, 0);
                        dt.Rows.Add(arrdr);
                    }

                    //生成excel
                    string FilePath = Server.MapPath("~/document/Excel/");
                    string file = dataToExcel.OutputExcel(dt, Cname, KAI, MO, payedID, "明细帐表", FilePath);
                    DownloadFile(file);
                    return;
                }
            }

            if (rbt3.Checked == true)
            {
                payedID = "='2' ";
                DataSet ds = new DataSet();
                if (gvTALL.Rows.Count > 0)
                {
                    DateTime KAI, MO;
                    if (tbGency.Text == "")
                    {
                        KAI = Convert.ToDateTime("1900-1-1").Date;
                    }
                    else
                    {
                        KAI = Convert.ToDateTime(tbGency.Text).Date;
                    }

                    if (tbGency1.Text == "")
                    {
                        MO = Convert.ToDateTime("2099-1-1").Date;
                    }
                    else
                        MO = Convert.ToDateTime(tbGency1.Text).Date;

                    string Cname = tbCus.Text;
                    //取得列名
                    ds = tALL.GetFeeWF(KAI, MO, Cname, payedID);
                    foreach (DataColumn dc in ds.Tables[0].Columns)
                    {
                        dt.Columns.Add(dc.ColumnName, dc.DataType);
                    }
                    //读取数据
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        object[] arrdr = new object[ds.Tables[0].Columns.Count];
                        ds.Tables[0].Rows[i].ItemArray.CopyTo(arrdr, 0);
                        dt.Rows.Add(arrdr);
                    }

                    //生成excel
                    string FilePath = Server.MapPath("~/document/Excel/");
                    string file = dataToExcel.OutputExcel(dt, Cname, KAI, MO, payedID, "明细帐表", FilePath);
                    DownloadFile(file);
                    return;
                }
            }
        }


        protected void DownloadFile(string file)
        {
            string filename = "/document/Excel/" + file;
            int intStart = filename.LastIndexOf(@"\") + 1;
            string saveFileName = filename.Substring(intStart, filename.Length - intStart);

            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.Default;
            //对文件进行编码，防止下载名出现乱码
            string encodefileName = System.Web.HttpUtility.UrlEncode(file, System.Text.Encoding.UTF8);

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodefileName);
            Response.ContentType = filecontenttype;

            Response.WriteFile(filename);
            Response.Flush();
            Response.Close();
            Response.End();
        }

        #region 页面跳转
        protected void YSshou_Click(object sender, EventArgs e)
        {
            Response.Redirect("YSshou.aspx");
        }

        protected void YSfu_Click(object sender, EventArgs e)
        {
            Response.Redirect("YSfu.aspx");
        }

        protected void btCheck_Click(object sender, EventArgs e)
        {
            Response.Redirect("Lirun.aspx");
        }

        protected void btqian_Click(object sender, EventArgs e)
        {
            Response.Redirect("FeeWS.aspx");
        }
        #endregion

        protected void gvTALL_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTALL.PageIndex = e.NewPageIndex;
            gvTALL.DataBind();
        }
    }
}